package com.etime.shuhuiyuan.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dto.SalerDto;
import com.etime.shuhuiyuan.pojo.Saler;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.sql.Wrapper;
import java.util.List;

@Repository
public interface SalerSelectDao extends BaseMapper<SalerDto> {

    @Select("select saler.*,sysuser.usertruename from saler" +
            " join sysuser on saler.userid=sysuser.userid")
    List<SalerDto> selectAllSaler(SalerDto salerDto);

//    @Select("select s.salerdeptid,s.salerid,s.salercode,s.salerbankname," +
//            "s.salerbanknum,s.salerauditstate,u.usertruename,d.deptname " +
//            "from saler s " +
//            "join sysuser u " +
//            "join department d " +
//            "on s.salerdeptid=d.deptid and s.userid=u.userid")

    /**
     * 分页查找-三表联查
     * @param page
     * @param wrapper
     * @return
     */
    @Select("select s.salerdeptid,s.salerid,s.salercode,s.salerbankname,"
            +" s.salerbanknum,s.salerauditstate,u.usertruename,d.deptname"
            +" from saler s join sysuser u on s.userid=u.userid"
            +" join department d on s.salerdeptid=d.deptid")
    Page<SalerDto> selectAllSalerDto(Page  page, QueryWrapper wrapper);

    /**
     * 根据id查找SalerDto
     * @param salerid
     * @return
     */
    @Select("select s.salerdeptid,s.salerid,s.salercode,s.salerbankname,"
            +" s.salerbanknum,s.salerauditstate,u.usertruename,d.deptname"
            +" from saler s join sysuser u on s.userid=u.userid"
            +" join department d on s.salerdeptid=d.deptid"
            +" where salerid=#{salerid}")
    SalerDto getSalerDtoById(Integer salerid);

    /**
     * 条件查询
     * @param page
     * @param salerDto
     * @param usertruename
     * @param salerauditstate
     * @return
     */
    @Select({"<script>"+
            "select s.salerdeptid,s.salerid,s.salercode,s.salerbankname,"+
                    "s.salerbanknum,s.salerauditdesc,s.salerauditstate,u.usertruename,d.deptname "+
                    "from saler s join sysuser u on s.userid=u.userid" +
                    " join department d on s.salerdeptid=d.deptid "+
            "<where>"+
            "<if test='usertruename!=null and usertruename!=\"\"'>"+
            "and u.usertruename like #{usertruename}"+
            "</if>"+
            "<if test='salerauditstate!=null'>"+
            "and s.salerauditstate=#{salerauditstate}"+
            "</if>"+
            "</where>"+
            "</script>"
    })
    Page<SalerDto> selectSalerDtoByCon(Page page, SalerDto salerDto, @Param("usertruename") String usertruename,@Param("salerauditstate") Integer salerauditstate);

}


